package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class TempNewBuiltTask
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/TempNewBuiltTask" })
public class TempNewBuiltTask extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userId=request.getParameter("userId");
		String task_type=request.getParameter("task_type");
		String task_name=request.getParameter("task_name");
		String project_name=request.getParameter("project_name");
		String task_character=request.getParameter("task_character");
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		 int taskId=0;
		try {
			connection = JdbcUtils.getConnection();
		   String sql = "insert into task(app_id_account,type,name,character,maintenance_project) values(?,?,?,?,?)";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userId);
			ps.setString(2, task_type);
			ps.setString(3, task_name);
			ps.setString(4, task_character);
			ps.setString(5, project_name);
		    ps.executeUpdate();
			
		    ps.close();
			sql ="select max(id) from task where task.app_id_account=? and task.type=? and task.name=?";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userId);
			ps.setString(2, task_type);
			ps.setString(3, task_name);
			rs=ps.executeQuery();
	     	if(rs.next())
		    {
	        taskId=rs.getInt(1);
	        result_jb.put("status", "true");
			result_jb.put("task_id",taskId );
			result_jb.put("message", "新建任务成功");
	        }else 
		   {
			result_jb.put("status", "false");
			result_jb.put("message", "没找到最大id数据");
			System.out.print("没找到最大id数据");
		  }
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
